#!/bin/bash

PIDFILE=/var/vcap/sys/run/foobar_with_bad_properties.pid

case $1 in

  start)
    echo "test_property=<%= p('test_property') %>"
    echo "az=<%= spec.az %>"
    echo "job_name=<%= spec.job.name %>"
    echo "index=<%= spec.index %>"
    echo "bootstrap=<%= spec.bootstrap %>"
    echo "raw_properties=<%= raw_properties %>"

    <% if_p('network_name') do |net_name| %>
      echo "<%= net_name %>_ip=<%= spec.networks.send(net_name).ip.strip %>"
    <% end %>
    echo "spec.address=<%= spec.address %>"

    <% if_p('networks') do |networks| %>
      <% networks.each do |net_name| %>
        echo "<%= net_name %>_ip=<%= spec.networks.send(net_name).ip.strip %>"
      <% end %>
    <% end %>

    echo $$ > $PIDFILE

    exec /bin/sh /var/vcap/packages/bar/run.sh
    ;;

  stop)
    tail -1 $PIDFILE | xargs kill

    rm -rf $PIDFILE
    ;;

  *)
  echo "Usage: foobar_ctl {start|stop}" ;;
esac

exit 0
